package com.sojson.project.controller.param.verify;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;

import com.sojson.enums.core.EDeliveryType;
import com.sojson.enums.core.EGeneralYesNo;
import com.sojson.project.entity.bo.OrderGoodsParam;
import com.sojson.project.entity.bo.OrderParam;
import com.sojson.project.entity.bo.OrderStoreParam;
import com.sojson.project.entity.po.Order;
import com.sojson.project.entity.vo.OrderVo;
import com.sojson.result.Result;
import com.sojson.util.ArithUtil;
import com.sojson.util.ExpUtil;
import com.sojson.util.MatcheUtil;
import com.sojson.util.StringUtil;

/**
 * 订单表参数验证
 * 
 * @author liu
 * @date 2021-03-16
 */
public class OrderParamVerify {

    /**
     * 下单(购买)
     * 
     * @param orderParam
     * @return
     */
    public static Result<?> ordersBuy(OrderParam orderParam) {
        if (StringUtil.isBlankObject(orderParam.getName())) {
            ExpUtil.throwEx("收货人不能为空！");
        }
        String mobile = orderParam.getMobile();
        if (StringUtil.isBlankObject(mobile)) {
            ExpUtil.throwEx("联系方式不能为空！");
        }
        if (!MatcheUtil.isMobile(mobile)) {
            return Result.fail("手机号格式错误！");
        }
        List<OrderStoreParam> orderStoreParams = orderParam.getOrderStoreParams();
        if (StringUtil.isBlankObject(orderStoreParams) || orderStoreParams.size() < 1) {
            ExpUtil.throwEx("商家订单参数集合不能为空！");
        }
        int isShop = orderStoreParams.size() > 1 ? EGeneralYesNo.YES.getCode() : EGeneralYesNo.NO.getCode();
        orderParam.setIsShop(isShop);
        if (orderStoreParams.size() > 1) {
            ExpUtil.throwEx("暂不支持购物车支付！");
        }
        for (OrderStoreParam orderStoreParam : orderStoreParams) {
            Integer typeDelivery = orderStoreParam.getTypeDelivery();
            if (StringUtil.isBlankObject(typeDelivery)) {
                ExpUtil.throwEx("配送类型不能为空！");
            }
            if (typeDelivery == EDeliveryType.SEND_OUT.getCode()) {
                if (StringUtil.isBlankObject(orderParam.getAddress())) {
                    ExpUtil.throwEx("详细地址不能为空！");
                }
            }
            orderStoreParam.setIsShop(isShop);
            String couponNos = orderStoreParam.getCouponNos();
            if (StringUtil.isBlankString(couponNos) || "0".equals(couponNos)) {
                orderStoreParam.setCouponNos(null);
            }
            List<OrderGoodsParam> orderGoodsParams = orderStoreParam.getOrderGoodsParams();
            if (StringUtil.isBlankObject(orderGoodsParams) || orderGoodsParams.size() < 1) {
                ExpUtil.throwEx("商品订单参数集合不能为空！");
            }
            if (orderGoodsParams.size() > 1) {
                ExpUtil.throwEx("购物车目前只能添加一种商品！");
            }
            for (OrderGoodsParam orderGoodsParam : orderGoodsParams) {
                orders(orderGoodsParam);
            }
        }

        return Result.success();
    }

    /**
     * 下单(新签)
     * 
     * @param orderParam
     * @return
     */
    public static Result<?> ordersNew(OrderParam orderParam) {
        List<OrderStoreParam> orderStoreParams = orderParam.getOrderStoreParams();
        if (StringUtil.isBlankObject(orderStoreParams) || orderStoreParams.size() < 1) {
            ExpUtil.throwEx("商家订单参数集合不能为空！");
        }
        for (OrderStoreParam orderStoreParam : orderStoreParams) {
            List<OrderGoodsParam> orderGoodsParams = orderStoreParam.getOrderGoodsParams();
            if (StringUtil.isBlankObject(orderGoodsParams) || orderGoodsParams.size() < 1) {
                ExpUtil.throwEx("商品订单参数集合不能为空！");
            }
            for (OrderGoodsParam orderGoodsParam : orderGoodsParams) {
                if (StringUtil.isBlankObject(orderGoodsParam.getGoodSpecsId())) {
                    ExpUtil.throwEx("商品规格组合ID不能为空！");
                }
                orders(orderGoodsParam);
            }
        }

        return Result.success();
    }

    /**
     * 下单
     * 
     * @param orderGoodsParam
     * @return
     */
    public static void orders(OrderGoodsParam orderGoodsParam) {
        if (StringUtil.isBlankObject(orderGoodsParam.getGoodSpecsId())) {
            ExpUtil.throwEx("商品规格组合ID不能为空！");
        }
        Integer goodNum = orderGoodsParam.getGoodNum();
        if (StringUtil.isBlankObject(goodNum) || goodNum < 1) {
            ExpUtil.throwEx("商品数量不能为空！");
        }
    }

    /**
     * 下单(续租)
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    public static Result<?> ordersGoOn(Order order) {
        if (StringUtil.isBlank(order.getId())) {
            return Result.fail("订单ID不能为空！");
        }
        if (StringUtil.isBlank(order.getPayType())) {
            return Result.fail("支付类型不能为空！");
        }
        if (StringUtil.isBlank(order.getRentDays())) {
            return Result.fail("租赁天数不能为空！");
        }

        return Result.success();
    }

    /**
     * 取消订单
     * 
     * @param no 订单编号
     * @return
     * @throws Exception 
     */
    public static Result<?> cancel(String no) {
        if (StringUtil.isBlank(no)) {
            return Result.fail("订单编号不能为空！");
        }

        return Result.success();
    }

    /**
     * 取消新签订单
     * 
     * @param id 订单ID
     * @return
     * @throws Exception 
     */
    public static Result<?> cancelNew(Integer id) {
        if (StringUtil.isBlank(id)) {
            return Result.fail("订单ID不能为空！");
        }

        return Result.success();
    }

    /**
     * 取消续租订单
     * 
     * @param id 订单ID
     * @return
     * @throws Exception 
     */
    public static Result<?> cancelGoOn(Integer id) {
        if (StringUtil.isBlank(id)) {
            return Result.fail("订单ID不能为空！");
        }

        return Result.success();
    }

    /**
     * 发货
     * 
     * @param order
     * @return
     * @throws Exception 
     */
    public static Result<?> sendOut(Order order) throws Exception {
        if (StringUtil.isBlank(order.getNo())) {
            return Result.fail("订单号不能为空！");
        }
        // if (StringUtil.isBlank(order.getExpressRentId())) {
        // return Result.fail("快递ID不能为空！");
        // }
        // String expressRentCode = order.getExpressRentCode();
        // if (StringUtil.isBlank(expressRentCode)) {
        // return Result.fail("快递单号不能为空！");
        // }
        // if (StringUtil.isBlank(order.getExpressRentName())) {
        // return Result.fail("快递名称不能为空！");
        // }
        // String expressRentShipperCode = order.getExpressRentShipperCode();
        // if (StringUtil.isBlank(expressRentShipperCode)) {
        // return Result.fail("快递公司编码不能为空！");
        // }
        // if (EShippers.JD.getCode().equals(expressRentShipperCode)
        // || EShippers.SF.getCode().equals(expressRentShipperCode)) {
        // if (StringUtil.isBlank(order.getExpressRentCustomerName())) {
        // return Result
        // .fail("租赁时顺丰快递传(寄件人/收件人手机号后四位),京东快递传商家编码(由发货方提供,且快递单号必须是通过该商家编码下单获取的)(为其他快递时,可不填或保留字段,不可传值)不能为空！");
        // }
        // }
        //
        // KdnParam kdnParam = new KdnParam();
        // kdnParam.setLogisticCode(expressRentCode);
        // kdnParam.setShipperCode(expressRentShipperCode);
        // KdnUtil.orderNoVerify(kdnParam);

        return Result.success();
    }

    public static Result<?> returnSendOut(Order order) throws Exception {
        if (StringUtil.isBlank(order.getNo())) {
            return Result.fail("订单号不能为空！");
        }
        // if (StringUtil.isBlank(order.getExpressBackId())) {
        // return Result.fail("快递ID不能为空！");
        // }
        // String expressBackCode = order.getExpressBackCode();
        // if (StringUtil.isBlank(expressBackCode)) {
        // return Result.fail("快递单号不能为空！");
        // }
        // if (StringUtil.isBlank(order.getExpressBackName())) {
        // return Result.fail("快递名称不能为空！");
        // }
        // String expressBackShipperCode = order.getExpressBackShipperCode();
        // if (StringUtil.isBlank(expressBackShipperCode)) {
        // return Result.fail("快递公司编码不能为空！");
        // }
        // if (EShippers.JD.getCode().equals(expressBackShipperCode)
        // || EShippers.SF.getCode().equals(expressBackShipperCode)) {
        // if (StringUtil.isBlank(order.getExpressBackCustomerName())) {
        // return Result
        // .fail("租赁时顺丰快递传(寄件人/收件人手机号后四位),京东快递传商家编码(由发货方提供,且快递单号必须是通过该商家编码下单获取的)(为其他快递时,可不填或保留字段,不可传值)不能为空！");
        // }
        // }
        //
        // KdnParam kdnParam = new KdnParam();
        // kdnParam.setLogisticCode(expressBackCode);
        // kdnParam.setShipperCode(expressBackShipperCode);
        // KdnUtil.orderNoVerify(kdnParam);

        return Result.success();
    }

    /**
     * 退款(租赁)
     * 
     * @param order 订单
     * @return
     * @throws Exception 
     */
    public static Result<?> refundNew(OrderVo order) {
        if (StringUtil.isBlankObject(order.getNo())) {
            return Result.fail("订单号不能为空！");
        }
        BigDecimal orderTotal = order.getOrderTotal();
        if (StringUtil.isBlankObject(orderTotal)) {
            return Result.fail("订单金额不能为空！");
        }
        int intValue = orderTotal.intValue();
        if (intValue <= 0) {
            return Result.fail("订单金额必须大于0空！");
        }
        order.setOrderTotal(ArithUtil.intPennyToBigDollar(intValue));

        return Result.success();
    }

    /**
     * 申请取消订单审核
     * 
     * @param order 订单
     * @return
     */
    public static Result<?> cancelAudit(OrderVo order) {
        if (StringUtil.isBlankObject(order.getId())) {
            return Result.fail("订单ID不能为空！");
        }
        if (StringUtil.isBlankObject(order.getCancelAudit())) {
            return Result.fail("审核状态不能为空！");
        }

        return Result.success();
    }

    /**
     * 后台订单退款
     * 
     * @param order 订单
     * @return
     */
    public static Result<?> refund(OrderVo order) {
        if (StringUtil.isBlankObject(order.getId())) {
            return Result.fail("订单ID不能为空！");
        }
        BigDecimal refundTotal = order.getRefundTotal();
        if (StringUtil.isBlankObject(refundTotal)) {
            return Result.fail("退款金额不能为空！");
        }
        int intValue = refundTotal.intValue();
        if (intValue < 1) {
            return Result.fail("退款金额必须大于0！");
        }
        order.setRefundTotal(ArithUtil.intPennyToBigDollar(intValue));

        return Result.success();
    }

}